using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class LevelController : MonoBehaviour
{
	public delegate void LevelEventHandler(Transform levelTransform);

	[HideInInspector]
	public bool optimized;

	[HideInInspector]
	public List<Collider> allColliders;

	[HideInInspector]
	public List<GameObject> allCombinedObjects;

	[HideInInspector]
	public List<GameObject> allModifiedObjects;

	private GameObject boxCollidersParent;

	private GameObject boxCollidersSlipperyParent;

	private GameObject capsuleCollidersParent;

	private GameObject sphereCollidersParent;

	private int cacheObjectCount;

	[method: MethodImpl(32)]
	public static event LevelEventHandler LevelInPlaceEvent;

	[method: MethodImpl(32)]
	public static event LevelEventHandler LevelReadyEvent;

	[method: MethodImpl(32)]
	public static event LevelEventHandler LevelFinishResetEvent;

	[method: MethodImpl(32)]
	public static event LevelEventHandler LevelFinishedBeforeEvent;

	[method: MethodImpl(32)]
	public static event LevelEventHandler LevelDestroyEvent;

	public static void FireLevelInPlace(Transform levelTransform)
	{
		if (LevelController.LevelInPlaceEvent != null)
		{
			LevelController.LevelInPlaceEvent(levelTransform);
		}
	}

	public static void FireLevelReady(Transform levelTransform)
	{
		if (LevelController.LevelReadyEvent != null)
		{
			LevelController.LevelReadyEvent(levelTransform);
		}
	}

	public static void FireLevelFinishReset(Transform levelTransform)
	{
		if (LevelController.LevelFinishResetEvent != null)
		{
			LevelController.LevelFinishResetEvent(levelTransform);
		}
	}

	public static void FireLevelFinishedBefore(Transform levelTransform)
	{
		if (LevelController.LevelFinishedBeforeEvent != null)
		{
			LevelController.LevelFinishedBeforeEvent(levelTransform);
		}
	}

	public static void FireLevelDestroy(Transform levelTransform)
	{
		if (LevelController.LevelDestroyEvent != null)
		{
			LevelController.LevelDestroyEvent(levelTransform);
		}
	}

	private void Awake()
	{
		Transform transform = base.transform.Find("CameraFly");
		if (transform != null)
		{
			Debug.Log("TEMPORARY: destroy cameraFly object ... de acolo vin warnigurile de missing reference");
			Object.Destroy(transform.gameObject);
		}
	}

	private void Start()
	{
		EventManager.AfterWorldResetEvent += OnAfterWorldReset;
	}

	private void OnDestroy()
	{
		FireLevelDestroy(base.transform);
		EventManager.AfterWorldResetEvent -= OnAfterWorldReset;
	}

	private void OnAfterWorldReset()
	{
		FireLevelFinishReset(base.transform);
	}

	public void StartLevel()
	{
		StartCoroutine(StartRoutine());
	}

	private IEnumerator StartRoutine()
	{
		yield return new WaitForEndOfFrame();
		SetInPlace();
		if (!optimized)
		{
			Optimize();
		}
		foreach (Collider coll in allColliders)
		{
			coll.enabled = true;
		}
		SetReady();
	}

	private IEnumerator StartCounter(string message)
	{
		Debug.Log(message + " in 3");
		yield return new WaitForSeconds(1f);
		Debug.Log(message + " in 2");
		yield return new WaitForSeconds(1f);
		Debug.Log(message + " in 1");
		yield return new WaitForSeconds(1f);
	}

	public void SetInPlace()
	{
		FireLevelInPlace(base.transform);
	}

	public void SetReady()
	{
		FireLevelReady(base.transform);
	}

	public GameObject GetStartObject()
	{
		Transform transform = base.transform.Find("Level Start");
		if (transform == null)
		{
			transform = base.transform.Find("Level Start Old");
		}
		if (transform != null)
		{
			return transform.gameObject;
		}
		return null;
	}

	public GameObject GetFinishObject()
	{
		Transform transform = base.transform.Find("Level Finish");
		if (transform != null)
		{
			return transform.gameObject;
		}
		return null;
	}

	public float GetLength()
	{
		GameObject startObject = GetStartObject();
		GameObject finishObject = GetFinishObject();
		if (startObject == null || finishObject == null)
		{
			return -1f;
		}
		return Vector3.Distance(startObject.transform.position, finishObject.transform.position);
	}

	public void Optimize()
	{
		GameObject gameObject = GameObject.Find("CameraFly");
		if (gameObject != null)
		{
			Object.DestroyImmediate(gameObject);
		}
		allColliders = new List<Collider>();
		allCombinedObjects = new List<GameObject>();
		allModifiedObjects = new List<GameObject>();
		CacheObjectCount();
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		boxCollidersParent = null;
		boxCollidersSlipperyParent = null;
		capsuleCollidersParent = null;
		sphereCollidersParent = null;
		CombineWalls();
		CombineFloors();
		CombineFloorsSlippery();
		CombineRampsFlat();
		CombineRampsCurved();
		CombineRampsCircular();
		CombinePipes();
		CombinePlatformBalanceNuts();
		CombineRollerNuts();
		CombineBumps();
		CombinePlatformHinge();
		CombineTrampoline();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			if (transform != null)
			{
				if (transform.gameObject.name == "FloorGateBack")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "FloorGateForward")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Balance")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Hinge")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Mobile")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Mobile Slope")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Mobile Slope Prisma 1")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Mobile Slope Prisma 2")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Mobile Slope Prisma 3")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Mobile Slope Prisma 4")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Platform Temporary")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Crate push")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
				else if (transform.gameObject.name == "Roller")
				{
					allModifiedObjects.Add(transform.gameObject);
				}
			}
		}
		if (boxCollidersParent != null)
		{
			boxCollidersParent.isStatic = true;
		}
		if (capsuleCollidersParent != null)
		{
			capsuleCollidersParent.isStatic = true;
		}
		if (sphereCollidersParent != null)
		{
			sphereCollidersParent.isStatic = true;
		}
		foreach (Collider allCollider in allColliders)
		{
			allCollider.enabled = false;
		}
		optimized = true;
		Debug.Log("OPTIMIZED '" + base.name + "' in " + (Time.realtimeSinceStartup - realtimeSinceStartup).ToString("f5"));
		Invoke("PrintObjectCount", 0.1f);
	}

	private void CombineWalls()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "Wall")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " walls ...");
		if (boxCollidersParent == null)
		{
			boxCollidersParent = new GameObject("Box Colliders Combined");
			boxCollidersParent.transform.parent = base.transform;
			boxCollidersParent.layer = 11;
		}
		GameObject gameObject = PackGameObjects(list);
		List<GameObject> list2 = new List<GameObject>();
		list2.Add(gameObject);
		GameObject gameObject2 = PackGameObjects(list2, 0, false);
		gameObject2.name = "Walls Combined";
		gameObject2.transform.parent = base.transform;
		gameObject2.GetComponent<Renderer>().castShadows = false;
		gameObject2.isStatic = true;
		allCombinedObjects.Add(gameObject2);
		foreach (GameObject item in list)
		{
			BoxCollider component = item.GetComponent<BoxCollider>();
			if (component != null)
			{
				if (item.transform.eulerAngles == Vector3.zero)
				{
					BoxCollider boxCollider = boxCollidersParent.AddComponent<BoxCollider>();
					boxCollider.center = item.transform.TransformPoint(component.center);
					boxCollider.size = new Vector3(component.size.x * item.transform.localScale.x, component.size.y * item.transform.localScale.y, component.size.z * item.transform.localScale.z);
					allColliders.Add(boxCollider);
					Object.DestroyImmediate(item);
				}
				else
				{
					Object.DestroyImmediate(item.GetComponent<MeshFilter>());
					Object.DestroyImmediate(item.GetComponent<Renderer>());
					item.transform.parent = boxCollidersParent.transform;
					item.isStatic = true;
					allColliders.Add(item.GetComponent<BoxCollider>());
				}
			}
		}
		Object.DestroyImmediate(gameObject);
	}

	private void CombineFloors()
	{
		List<GameObject> list = new List<GameObject>();
		List<GameObject> list2 = new List<GameObject>();
		List<GameObject> list3 = new List<GameObject>();
		List<GameObject> list4 = new List<GameObject>();
		List<GameObject> list5 = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "FloorBlock" || transform.name == "FloorSlope")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism1")
			{
				list2.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism2")
			{
				list3.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism3")
			{
				list4.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism4")
			{
				list5.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count > 0)
		{
			Debug.Log("Combine " + list.Count + " floors ...");
			if (boxCollidersParent == null)
			{
				boxCollidersParent = new GameObject("Box Colliders Combined");
				boxCollidersParent.transform.parent = base.transform;
				boxCollidersParent.layer = 11;
			}
			GameObject gameObject = PackGameObjects(list);
			GameObject gameObject2 = PackGameObjects(list, 1);
			GameObject gameObject3 = PackGameObjects(list, 2);
			GameObject gameObject4 = PackGameObjects(list, 3);
			List<GameObject> list6 = new List<GameObject>();
			list6.Add(gameObject);
			list6.Add(gameObject2);
			list6.Add(gameObject3);
			list6.Add(gameObject4);
			GameObject gameObject5 = PackGameObjects(list6, 0, false);
			gameObject5.name = "Floors Combined";
			gameObject5.transform.parent = base.transform;
			gameObject5.GetComponent<Renderer>().castShadows = false;
			gameObject5.isStatic = true;
			allCombinedObjects.Add(gameObject5);
			foreach (GameObject item in list)
			{
				BoxCollider component = item.GetComponent<BoxCollider>();
				if (!(component != null))
				{
					continue;
				}
				if (item.transform.eulerAngles == Vector3.zero)
				{
					BoxCollider boxCollider = boxCollidersParent.AddComponent<BoxCollider>();
					boxCollider.center = item.transform.TransformPoint(component.center);
					boxCollider.size = new Vector3(component.size.x * item.transform.localScale.x, component.size.y * item.transform.localScale.y, component.size.z * item.transform.localScale.z);
					allColliders.Add(boxCollider);
					Object.DestroyImmediate(item);
				}
				else if (item.transform.eulerAngles.x == 0f && item.transform.eulerAngles.z == 0f && item.transform.eulerAngles.y % 90f < 1f)
				{
					if (Mathf.Abs(item.transform.eulerAngles.y) < 45f || (Mathf.Abs(item.transform.eulerAngles.y) > 135f && Mathf.Abs(item.transform.eulerAngles.y) < 215f))
					{
						BoxCollider boxCollider2 = boxCollidersParent.AddComponent<BoxCollider>();
						boxCollider2.center = item.transform.TransformPoint(component.center);
						boxCollider2.size = new Vector3(component.size.x * item.transform.localScale.x, component.size.y * item.transform.localScale.y, component.size.z * item.transform.localScale.z);
						allColliders.Add(boxCollider2);
					}
					else
					{
						BoxCollider boxCollider3 = boxCollidersParent.AddComponent<BoxCollider>();
						boxCollider3.center = item.transform.TransformPoint(component.center);
						boxCollider3.size = new Vector3(component.size.z * item.transform.localScale.z, component.size.y * item.transform.localScale.y, component.size.x * item.transform.localScale.x);
						allColliders.Add(boxCollider3);
					}
					Object.DestroyImmediate(item);
				}
				else
				{
					Object.DestroyImmediate(item.GetComponent<MeshFilter>());
					Object.DestroyImmediate(item.GetComponent<Renderer>());
					item.transform.parent = boxCollidersParent.transform;
					item.isStatic = true;
					allColliders.Add(item.GetComponent<BoxCollider>());
				}
			}
			Object.DestroyImmediate(gameObject);
			Object.DestroyImmediate(gameObject2);
			Object.DestroyImmediate(gameObject3);
			Object.DestroyImmediate(gameObject4);
		}
		if (list2.Count <= 0 || list3.Count <= 0 || list4.Count <= 0 || list5.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list2.Count + " prisms ...");
		if (boxCollidersParent == null)
		{
			boxCollidersParent = new GameObject("Box Colliders Combined");
			boxCollidersParent.transform.parent = base.transform;
			boxCollidersParent.layer = 11;
		}
		List<GameObject> list7 = new List<GameObject>();
		List<int> list8 = new List<int>();
		list7.Clear();
		list8.Clear();
		foreach (GameObject item2 in list2)
		{
			list7.Add(item2);
			list8.Add(0);
			list7.Add(item2);
			list8.Add(2);
		}
		foreach (GameObject item3 in list3)
		{
			list7.Add(item3);
			list8.Add(2);
		}
		foreach (GameObject item4 in list4)
		{
			list7.Add(item4);
			list8.Add(2);
		}
		foreach (GameObject item5 in list5)
		{
			list7.Add(item5);
			list8.Add(0);
			list7.Add(item5);
			list8.Add(2);
		}
		GameObject gameObject6 = PackGameObjects(list7, list8);
		list7.Clear();
		list8.Clear();
		foreach (GameObject item6 in list2)
		{
			list7.Add(item6);
			list8.Add(1);
		}
		foreach (GameObject item7 in list3)
		{
			list7.Add(item7);
			list8.Add(0);
			list7.Add(item7);
			list8.Add(1);
		}
		GameObject gameObject7 = PackGameObjects(list7, list8);
		list7.Clear();
		list8.Clear();
		foreach (GameObject item8 in list4)
		{
			list7.Add(item8);
			list8.Add(0);
			list7.Add(item8);
			list8.Add(1);
		}
		foreach (GameObject item9 in list5)
		{
			list7.Add(item9);
			list8.Add(1);
		}
		GameObject gameObject8 = PackGameObjects(list7, list8);
		List<GameObject> list9 = new List<GameObject>();
		list9.Add(gameObject6);
		list9.Add(gameObject7);
		list9.Add(gameObject8);
		GameObject gameObject9 = PackGameObjects(list9, 0, false);
		gameObject9.name = "Prisms Combined";
		gameObject9.transform.parent = base.transform;
		gameObject9.GetComponent<Renderer>().castShadows = false;
		gameObject9.isStatic = true;
		allCombinedObjects.Add(gameObject9);
		foreach (GameObject item10 in list2)
		{
			Object.DestroyImmediate(item10.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item10.GetComponent<Renderer>());
			item10.transform.parent = boxCollidersParent.transform;
			item10.isStatic = true;
			allColliders.Add(item10.GetComponent<BoxCollider>());
		}
		foreach (GameObject item11 in list3)
		{
			Object.DestroyImmediate(item11.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item11.GetComponent<Renderer>());
			item11.transform.parent = boxCollidersParent.transform;
			item11.isStatic = true;
			allColliders.Add(item11.GetComponent<BoxCollider>());
		}
		foreach (GameObject item12 in list4)
		{
			Object.DestroyImmediate(item12.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item12.GetComponent<Renderer>());
			item12.transform.parent = boxCollidersParent.transform;
			item12.isStatic = true;
			allColliders.Add(item12.GetComponent<BoxCollider>());
		}
		foreach (GameObject item13 in list5)
		{
			Object.DestroyImmediate(item13.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item13.GetComponent<Renderer>());
			item13.transform.parent = boxCollidersParent.transform;
			item13.isStatic = true;
			allColliders.Add(item13.GetComponent<BoxCollider>());
		}
		Object.DestroyImmediate(gameObject6);
		Object.DestroyImmediate(gameObject7);
		Object.DestroyImmediate(gameObject8);
	}

	private void CombineFloorsSlippery()
	{
		List<GameObject> list = new List<GameObject>();
		List<GameObject> list2 = new List<GameObject>();
		List<GameObject> list3 = new List<GameObject>();
		List<GameObject> list4 = new List<GameObject>();
		List<GameObject> list5 = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "FloorBlockSlippery" || transform.name == "FloorSlopeSlippery")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism1Slippery")
			{
				list2.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism2Slippery")
			{
				list3.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism3Slippery")
			{
				list4.Add(transform.gameObject);
				flag = true;
			}
			else if (transform.name == "FloorSlopePrism4Slippery")
			{
				list5.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count > 0)
		{
			Debug.Log("Combine " + list.Count + " floorsSlippery ...");
			if (boxCollidersSlipperyParent == null)
			{
				boxCollidersSlipperyParent = new GameObject("Box Colliders Slippery Combined");
				boxCollidersSlipperyParent.transform.parent = base.transform;
				boxCollidersSlipperyParent.tag = "slippery";
				boxCollidersSlipperyParent.layer = 11;
			}
			GameObject gameObject = PackGameObjects(list);
			GameObject gameObject2 = PackGameObjects(list, 1);
			GameObject gameObject3 = PackGameObjects(list, 2);
			GameObject gameObject4 = PackGameObjects(list, 3);
			List<GameObject> list6 = new List<GameObject>();
			list6.Add(gameObject);
			list6.Add(gameObject2);
			list6.Add(gameObject3);
			list6.Add(gameObject4);
			GameObject gameObject5 = PackGameObjects(list6, 0, false);
			gameObject5.name = "Floors Slippery Combined";
			gameObject5.transform.parent = base.transform;
			gameObject5.GetComponent<Renderer>().castShadows = false;
			gameObject5.isStatic = true;
			allCombinedObjects.Add(gameObject5);
			foreach (GameObject item in list)
			{
				BoxCollider component = item.GetComponent<BoxCollider>();
				if (!(component != null))
				{
					continue;
				}
				if (item.transform.eulerAngles == Vector3.zero)
				{
					BoxCollider boxCollider = boxCollidersSlipperyParent.AddComponent<BoxCollider>();
					boxCollider.center = item.transform.TransformPoint(component.center);
					boxCollider.size = new Vector3(component.size.x * item.transform.localScale.x, component.size.y * item.transform.localScale.y, component.size.z * item.transform.localScale.z);
					boxCollider.sharedMaterial = component.sharedMaterial;
					allColliders.Add(boxCollider);
					Object.DestroyImmediate(item);
				}
				else if (item.transform.eulerAngles.x == 0f && item.transform.eulerAngles.z == 0f && item.transform.eulerAngles.y % 90f < 1f)
				{
					if (Mathf.Abs(item.transform.eulerAngles.y) < 45f || (Mathf.Abs(item.transform.eulerAngles.y) > 135f && Mathf.Abs(item.transform.eulerAngles.y) < 215f))
					{
						BoxCollider boxCollider2 = boxCollidersSlipperyParent.AddComponent<BoxCollider>();
						boxCollider2.center = item.transform.TransformPoint(component.center);
						boxCollider2.size = new Vector3(component.size.x * item.transform.localScale.x, component.size.y * item.transform.localScale.y, component.size.z * item.transform.localScale.z);
						boxCollider2.sharedMaterial = component.sharedMaterial;
						allColliders.Add(boxCollider2);
					}
					else
					{
						BoxCollider boxCollider3 = boxCollidersSlipperyParent.AddComponent<BoxCollider>();
						boxCollider3.center = item.transform.TransformPoint(component.center);
						boxCollider3.size = new Vector3(component.size.z * item.transform.localScale.z, component.size.y * item.transform.localScale.y, component.size.x * item.transform.localScale.x);
						boxCollider3.sharedMaterial = component.sharedMaterial;
						allColliders.Add(boxCollider3);
					}
					Object.DestroyImmediate(item);
				}
				else
				{
					Object.DestroyImmediate(item.GetComponent<MeshFilter>());
					Object.DestroyImmediate(item.GetComponent<Renderer>());
					item.transform.parent = boxCollidersSlipperyParent.transform;
					item.isStatic = true;
					allColliders.Add(item.GetComponent<BoxCollider>());
				}
			}
			Object.DestroyImmediate(gameObject);
			Object.DestroyImmediate(gameObject2);
			Object.DestroyImmediate(gameObject3);
			Object.DestroyImmediate(gameObject4);
		}
		if (list2.Count <= 0 || list3.Count <= 0 || list4.Count <= 0 || list5.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list2.Count + " prisms Slippery ...");
		if (boxCollidersSlipperyParent == null)
		{
			boxCollidersSlipperyParent = new GameObject("Box Colliders Slippery Combined");
			boxCollidersSlipperyParent.transform.parent = base.transform;
			boxCollidersSlipperyParent.tag = "slippery";
			boxCollidersSlipperyParent.layer = 11;
		}
		List<GameObject> list7 = new List<GameObject>();
		List<int> list8 = new List<int>();
		list7.Clear();
		list8.Clear();
		foreach (GameObject item2 in list2)
		{
			list7.Add(item2);
			list8.Add(0);
			list7.Add(item2);
			list8.Add(2);
		}
		foreach (GameObject item3 in list3)
		{
			list7.Add(item3);
			list8.Add(2);
		}
		foreach (GameObject item4 in list4)
		{
			list7.Add(item4);
			list8.Add(2);
		}
		foreach (GameObject item5 in list5)
		{
			list7.Add(item5);
			list8.Add(0);
			list7.Add(item5);
			list8.Add(2);
		}
		GameObject gameObject6 = PackGameObjects(list7, list8);
		list7.Clear();
		list8.Clear();
		foreach (GameObject item6 in list2)
		{
			list7.Add(item6);
			list8.Add(1);
		}
		foreach (GameObject item7 in list3)
		{
			list7.Add(item7);
			list8.Add(0);
			list7.Add(item7);
			list8.Add(1);
		}
		GameObject gameObject7 = PackGameObjects(list7, list8);
		list7.Clear();
		list8.Clear();
		foreach (GameObject item8 in list4)
		{
			list7.Add(item8);
			list8.Add(0);
			list7.Add(item8);
			list8.Add(1);
		}
		foreach (GameObject item9 in list5)
		{
			list7.Add(item9);
			list8.Add(1);
		}
		GameObject gameObject8 = PackGameObjects(list7, list8);
		List<GameObject> list9 = new List<GameObject>();
		list9.Add(gameObject6);
		list9.Add(gameObject7);
		list9.Add(gameObject8);
		GameObject gameObject9 = PackGameObjects(list9, 0, false);
		gameObject9.name = "Prisms Slippery Combined";
		gameObject9.transform.parent = base.transform;
		gameObject9.GetComponent<Renderer>().castShadows = false;
		gameObject9.isStatic = true;
		allCombinedObjects.Add(gameObject9);
		foreach (GameObject item10 in list2)
		{
			Object.DestroyImmediate(item10.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item10.GetComponent<Renderer>());
			item10.transform.parent = boxCollidersSlipperyParent.transform;
			item10.isStatic = true;
			allColliders.Add(item10.GetComponent<BoxCollider>());
		}
		foreach (GameObject item11 in list3)
		{
			Object.DestroyImmediate(item11.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item11.GetComponent<Renderer>());
			item11.transform.parent = boxCollidersSlipperyParent.transform;
			item11.isStatic = true;
			allColliders.Add(item11.GetComponent<BoxCollider>());
		}
		foreach (GameObject item12 in list4)
		{
			Object.DestroyImmediate(item12.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item12.GetComponent<Renderer>());
			item12.transform.parent = boxCollidersSlipperyParent.transform;
			item12.isStatic = true;
			allColliders.Add(item12.GetComponent<BoxCollider>());
		}
		foreach (GameObject item13 in list5)
		{
			Object.DestroyImmediate(item13.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item13.GetComponent<Renderer>());
			item13.transform.parent = boxCollidersSlipperyParent.transform;
			item13.isStatic = true;
			allColliders.Add(item13.GetComponent<BoxCollider>());
		}
		Object.DestroyImmediate(gameObject6);
		Object.DestroyImmediate(gameObject7);
		Object.DestroyImmediate(gameObject8);
	}

	private void CombineRampsFlat()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "RampFlat")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " rampsFlat ...");
		GameObject gameObject = PackGameObjects(list);
		GameObject gameObject2 = PackGameObjects(list, 1);
		List<GameObject> list2 = new List<GameObject>();
		list2.Add(gameObject);
		list2.Add(gameObject2);
		GameObject gameObject3 = PackGameObjects(list2, 0, false);
		gameObject3.name = "Ramps Flat Combined";
		gameObject3.transform.parent = base.transform;
		allColliders.Add(gameObject3.AddComponent<MeshCollider>());
		gameObject3.layer = 11;
		gameObject3.GetComponent<Renderer>().castShadows = false;
		gameObject3.isStatic = true;
		allCombinedObjects.Add(gameObject3);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item);
		}
		Object.DestroyImmediate(gameObject);
		Object.DestroyImmediate(gameObject2);
	}

	private void CombineRampsCurved()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "RampCurved")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " rampsCurved ...");
		GameObject gameObject = PackGameObjects(list);
		GameObject gameObject2 = PackGameObjects(list, 1);
		List<GameObject> list2 = new List<GameObject>();
		list2.Add(gameObject);
		list2.Add(gameObject2);
		GameObject gameObject3 = PackGameObjects(list2, 0, false);
		gameObject3.name = "Ramps Curved Combined";
		gameObject3.transform.parent = base.transform;
		allColliders.Add(gameObject3.AddComponent<MeshCollider>());
		gameObject3.layer = 11;
		gameObject3.GetComponent<Renderer>().castShadows = false;
		gameObject3.isStatic = true;
		allCombinedObjects.Add(gameObject3);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item);
		}
		Object.DestroyImmediate(gameObject);
		Object.DestroyImmediate(gameObject2);
	}

	private void CombineRampsCircular()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "RampCircular")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " rampsCircular ...");
		GameObject gameObject = PackGameObjects(list);
		GameObject gameObject2 = PackGameObjects(list, 1);
		List<GameObject> list2 = new List<GameObject>();
		list2.Add(gameObject);
		list2.Add(gameObject2);
		GameObject gameObject3 = PackGameObjects(list2, 0, false);
		gameObject3.name = "Ramps Circular Combined";
		gameObject3.transform.parent = base.transform;
		allColliders.Add(gameObject3.AddComponent<MeshCollider>());
		gameObject3.layer = 11;
		gameObject3.GetComponent<Renderer>().castShadows = false;
		gameObject3.isStatic = true;
		allCombinedObjects.Add(gameObject3);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item);
		}
		Object.DestroyImmediate(gameObject);
		Object.DestroyImmediate(gameObject2);
	}

	private void CombinePipes()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name.StartsWith("Pipe"))
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " pipes ...");
		if (capsuleCollidersParent == null)
		{
			capsuleCollidersParent = new GameObject("Capsule Colliders Combined");
			capsuleCollidersParent.transform.parent = base.transform;
			capsuleCollidersParent.layer = 13;
		}
		GameObject gameObject = PackGameObjects(list);
		gameObject.name = "Pipes Combined";
		gameObject.transform.parent = base.transform;
		gameObject.GetComponent<Renderer>().castShadows = false;
		gameObject.isStatic = true;
		allCombinedObjects.Add(gameObject);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item.GetComponent<MeshFilter>());
			Object.DestroyImmediate(item.GetComponent<Renderer>());
			item.transform.parent = capsuleCollidersParent.transform;
			item.isStatic = true;
			allColliders.Add(item.GetComponent<Collider>());
		}
	}

	private void CombineBumps()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "bump")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " bumps ...");
		if (sphereCollidersParent == null)
		{
			sphereCollidersParent = new GameObject("Sphere Colliders Combined");
			sphereCollidersParent.transform.parent = base.transform;
			sphereCollidersParent.layer = 11;
		}
		GameObject gameObject = PackGameObjects(list);
		gameObject.name = "Bumps Combined";
		gameObject.transform.parent = base.transform;
		gameObject.GetComponent<Renderer>().castShadows = false;
		gameObject.isStatic = true;
		allCombinedObjects.Add(gameObject);
		foreach (GameObject item in list)
		{
			SphereCollider component = item.GetComponent<SphereCollider>();
			SphereCollider sphereCollider = sphereCollidersParent.AddComponent<SphereCollider>();
			sphereCollider.center = item.transform.TransformPoint(component.center);
			sphereCollider.radius = component.radius * item.transform.localScale.x;
			Object.DestroyImmediate(item);
			allColliders.Add(sphereCollider);
		}
	}

	private void CombinePlatformHinge()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "Platform Hinge Base")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " Platform Hinge Bases ...");
		GameObject gameObject = PackGameObjects(list);
		gameObject.name = "Platform Hinge Base Combined";
		gameObject.transform.parent = base.transform;
		gameObject.GetComponent<Renderer>().castShadows = false;
		gameObject.isStatic = true;
		allCombinedObjects.Add(gameObject);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item);
		}
	}

	private void CombineTrampoline()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name == "TrampolineOutside")
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " TrampolineOutside ...");
		GameObject gameObject = PackGameObjects(list);
		gameObject.name = "TrampolineOutside Combined";
		gameObject.transform.parent = base.transform;
		gameObject.GetComponent<Renderer>().castShadows = false;
		gameObject.isStatic = true;
		allCombinedObjects.Add(gameObject);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item);
		}
	}

	private void CombinePlatformBalanceNuts()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name.StartsWith("Platform Balance Nut"))
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " Balance Nuts ...");
		GameObject gameObject = PackGameObjects(list);
		GameObject gameObject2 = PackGameObjects(list, 1);
		List<GameObject> list2 = new List<GameObject>();
		list2.Add(gameObject);
		list2.Add(gameObject2);
		GameObject gameObject3 = PackGameObjects(list2, 0, false);
		gameObject3.name = "Balance Nuts Combined";
		gameObject3.transform.parent = base.transform;
		gameObject3.layer = 11;
		gameObject3.GetComponent<Renderer>().castShadows = false;
		gameObject3.isStatic = true;
		allCombinedObjects.Add(gameObject3);
		foreach (GameObject item in list)
		{
			Object.DestroyImmediate(item);
		}
		Object.DestroyImmediate(gameObject);
		Object.DestroyImmediate(gameObject2);
	}

	private void CombineRollerNuts()
	{
		List<GameObject> list = new List<GameObject>();
		Transform[] componentsInChildren = base.transform.GetComponentsInChildren<Transform>();
		foreach (Transform transform in componentsInChildren)
		{
			bool flag = false;
			if (transform.name.StartsWith("Roller Nut"))
			{
				list.Add(transform.gameObject);
				flag = true;
			}
			if (flag)
			{
				transform.gameObject.isStatic = false;
			}
		}
		if (list.Count <= 0)
		{
			return;
		}
		Debug.Log("Combine " + list.Count + " Roller Nuts ...");
		if (sphereCollidersParent == null)
		{
			sphereCollidersParent = new GameObject("Sphere Colliders Combined");
			sphereCollidersParent.transform.parent = base.transform;
			sphereCollidersParent.layer = 11;
		}
		GameObject gameObject = PackGameObjects(list);
		gameObject.name = "Roller Nuts Combined";
		gameObject.transform.parent = base.transform;
		gameObject.layer = 11;
		gameObject.GetComponent<Renderer>().castShadows = false;
		gameObject.isStatic = true;
		allCombinedObjects.Add(gameObject);
		foreach (GameObject item in list)
		{
			SphereCollider component = item.GetComponent<SphereCollider>();
			SphereCollider sphereCollider = sphereCollidersParent.AddComponent<SphereCollider>();
			sphereCollider.center = item.transform.TransformPoint(component.center);
			sphereCollider.radius = component.radius * item.transform.localScale.y;
			Object.DestroyImmediate(item);
			allColliders.Add(sphereCollider);
		}
	}

	private void PrintObjectCount()
	{
		int objectCount = GetObjectCount();
		Debug.Log("GameObjects in level '" + base.name + "' = " + objectCount + " (before optimization = " + cacheObjectCount + ")");
	}

	private void CacheObjectCount()
	{
		cacheObjectCount = GetObjectCount();
	}

	private int GetObjectCount()
	{
		return base.transform.GetComponentsInChildren<Transform>().Length;
	}

	public GameObject PackGameObjects(List<GameObject> gameObjects, List<int> subMeshIndexes, bool mergeSubMeshes = true)
	{
		GameObject gameObject = new GameObject("Packed Multiple");
		CombineInstance[] array = new CombineInstance[gameObjects.Count];
		MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
		MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
		Material[] array2 = new Material[gameObjects.Count];
		for (int i = 0; i < gameObjects.Count; i++)
		{
			MeshFilter component = gameObjects[i].GetComponent<MeshFilter>();
			array[i].mesh = component.mesh;
			array[i].subMeshIndex = subMeshIndexes[i];
			array[i].transform = component.transform.localToWorldMatrix;
			array2[i] = gameObjects[i].GetComponent<Renderer>().sharedMaterial;
		}
		meshFilter.mesh.CombineMeshes(array, mergeSubMeshes);
		if (mergeSubMeshes)
		{
			meshRenderer.sharedMaterial = gameObjects[0].GetComponent<Renderer>().sharedMaterials[subMeshIndexes[0]];
		}
		else
		{
			meshRenderer.sharedMaterials = array2;
		}
		return gameObject;
	}

	public GameObject PackGameObjects(List<GameObject> gameObjects, int subMeshIndex = 0, bool mergeSubMeshes = true)
	{
		GameObject gameObject = new GameObject("Packed " + subMeshIndex);
		CombineInstance[] array = new CombineInstance[gameObjects.Count];
		MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
		MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer>();
		Material[] array2 = new Material[gameObjects.Count];
		for (int i = 0; i < gameObjects.Count; i++)
		{
			MeshFilter component = gameObjects[i].GetComponent<MeshFilter>();
			if (component.mesh == null)
			{
				GameObject gameObject2 = Object.Instantiate(gameObjects[i]) as GameObject;
				gameObject2.transform.parent = null;
			}
			array[i].mesh = component.mesh;
			array[i].subMeshIndex = subMeshIndex;
			array[i].transform = component.transform.localToWorldMatrix;
			array2[i] = gameObjects[i].GetComponent<Renderer>().sharedMaterial;
		}
		meshFilter.mesh.CombineMeshes(array, mergeSubMeshes);
		if (mergeSubMeshes)
		{
			meshRenderer.sharedMaterial = gameObjects[0].GetComponent<Renderer>().sharedMaterials[subMeshIndex];
		}
		else
		{
			meshRenderer.sharedMaterials = array2;
		}
		return gameObject;
	}

	public void OrderCheckpoints()
	{
		ControllerCheckpointInstant[] componentsInChildren = base.gameObject.GetComponentsInChildren<ControllerCheckpointInstant>();
		if (componentsInChildren == null || componentsInChildren.Length == 0)
		{
			Debug.Log("No checkpoints found!");
			return;
		}
		Debug.Log("Found " + componentsInChildren.Length + " checkpoints");
		List<ControllerCheckpointInstant> list = new List<ControllerCheckpointInstant>();
		List<int> list2 = new List<int>();
		ControllerLevelStart componentInChildren = base.gameObject.GetComponentInChildren<ControllerLevelStart>();
		Vector3 b = Vector3.zero;
		if (componentInChildren != null)
		{
			b = componentInChildren.transform.position;
		}
		ControllerCheckpointInstant[] array = componentsInChildren;
		foreach (ControllerCheckpointInstant controllerCheckpointInstant in array)
		{
			RemoteControl remoteControl = controllerCheckpointInstant.remoteControl;
			if (remoteControl == null)
			{
				Debug.Log("Did not find remote for " + controllerCheckpointInstant);
				continue;
			}
			int num = 0;
			RemoteControl placementParent = remoteControl.placementParent;
			RemoteControl remoteControl2 = remoteControl;
			while (placementParent != null)
			{
				num++;
				remoteControl2 = placementParent;
				placementParent = remoteControl2.placementParent;
			}
			list.Add(controllerCheckpointInstant);
			list2.Add(num);
		}
		for (int j = 0; j < list.Count; j++)
		{
			for (int k = 1; k < list.Count; k++)
			{
				if (list2[k] < list2[k - 1])
				{
					int value = list2[k - 1];
					list2[k - 1] = list2[k];
					list2[k] = value;
					ControllerCheckpointInstant value2 = list[k - 1];
					list[k - 1] = list[k];
					list[k] = value2;
				}
			}
		}
		for (int l = 0; l < list.Count - 1; l++)
		{
			int m;
			for (m = l + 1; m < list.Count && list2[m] == list2[l]; m++)
			{
			}
			m--;
			if (m == l)
			{
				continue;
			}
			for (int n = l; n <= m; n++)
			{
				for (int num2 = l + 1; num2 <= m; num2++)
				{
					float num3 = Vector3.Distance(list[num2 - 1].transform.position, b);
					float num4 = Vector3.Distance(list[num2].transform.position, b);
					if (num4 < num3)
					{
						int value = list2[num2 - 1];
						list2[num2 - 1] = list2[num2];
						list2[num2] = value;
						ControllerCheckpointInstant value2 = list[num2 - 1];
						list[num2 - 1] = list[num2];
						list[num2] = value2;
					}
					else if (num4 == num3)
					{
						Vector3 position = list[num2 - 1].transform.position;
						Vector3 position2 = list[num2].transform.position;
						if (position2.x < position.x)
						{
							int value = list2[num2 - 1];
							list2[num2 - 1] = list2[num2];
							list2[num2] = value;
							ControllerCheckpointInstant value2 = list[num2 - 1];
							list[num2 - 1] = list[num2];
							list[num2] = value2;
						}
						else if (position2.y < position.y)
						{
							int value = list2[num2 - 1];
							list2[num2 - 1] = list2[num2];
							list2[num2] = value;
							ControllerCheckpointInstant value2 = list[num2 - 1];
							list[num2 - 1] = list[num2];
							list[num2] = value2;
						}
						else if (position2.z < position.z)
						{
							int value = list2[num2 - 1];
							list2[num2 - 1] = list2[num2];
							list2[num2] = value;
							ControllerCheckpointInstant value2 = list[num2 - 1];
							list[num2 - 1] = list[num2];
							list[num2] = value2;
						}
					}
				}
			}
			b = list[m].transform.position;
			l = m + 1;
		}
		for (int num5 = 0; num5 < list.Count; num5++)
		{
			list[num5].checkPointNumber = num5 + 1;
			Debug.Log(string.Concat("setting ", list[num5], " to ", num5 + 1));
		}
	}
}
